package pe.edu.pucp.cpnc.mm.bean;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Unit extends Object {	
	private int hp;
	private int atk;
	private int def;
	
	public Unit(){
		
	}
	
	public Unit(int id, String name, int type, int path, int sizeX, int sizeY, ArrayList<String> sprite, int hp, int atk, int def){
		super(id, name, type, path, sizeX, sizeY, sprite);
		this.hp = hp;
		this.atk = atk;
		this.def = def;
	}
	
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
	public int getAtk() {
		return atk;
	}
	public void setAtk(int atk) {
		this.atk = atk;
	}
	public int getDef() {
		return def;
	}
	public void setDef(int def) {
		this.def = def;
	}
	
	public int add(int posX, int posY, Object object, Map map , int idPlayer){
		return add(posX, posY, object, map, idPlayer, 0);
	}
	
	public int add(int posX, int posY, Object object, Map map , int idPlayer, int orientation){
		
		Unit u = (Unit)object;
		int sX = u.getSizeX(); //Width Limit
		int sY = u.getSizeY(); //Height Limit
		
		if(orientation == 1 || orientation == 3){
			int aux = sX;
			sX = sY;
			sY = aux;
		}
		
		for(int i=posY ;i<posY + sY; i++){
			for(int j=posX;j<posX + sX;j++){
				if(!(i>=0 && i<map.getSizeY() && j>=0 && j<map.getSizeX())){
					System.out.println("El objeto esta fuera de los limites.");
					return -1;
				}
				if(!(u.getPath() ==  map.getTiles()[i][j].getTile().getPath())){
					System.out.println("El objeto no puede atravesar este tile.");
					return -1;
				}
				if (map.getTiles()[i][j].getOccupied()==1){
					System.out.println("El tile esta ocupado.");
					return -1;
				}
			}
		}
		
		ObjectDetail objDetail = new ObjectDetail();
		
		objDetail.setObject(u);
		objDetail.setPosX(posX);
		objDetail.setPosY(posY);
		objDetail.setOrientation(orientation);
		
		map.getPlayers().get(idPlayer).getObjectDetails().add(objDetail);

		for(int i=posY;i<posY + sY;i++){ //Set tile Occupied
			for(int j=posX;j<sX + posX;j++){
				map.getTiles()[i][j].setOccupied(1);
                                map.getTiles()[i][j].setObjInside(objDetail);
			}
		}
                map.getTiles()[posY][posX].setIsRoot(1);
		return 0;		
	}
	
	
}
